package com.ruoyi.web.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class CredentialsNumberUtil {

    public static Boolean isCredentialsNumber(String credentialsNumber){
        return Pattern.matches("^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$",credentialsNumber)||
                Pattern.matches("^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$",credentialsNumber);
    }


    public static Date getBirthday(String credentialsNumber) {
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");

        if(credentialsNumber.length()==18){
            try {
                date = simpleDateFormat.parse(credentialsNumber.substring(6,14));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if(credentialsNumber.length()==15){
            try {
                date = simpleDateFormat.parse("19"+credentialsNumber.substring(6,12));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }


        return date;
    }


    public static int getSex(String credentialsNumber) {
        int sex=1;
        if(credentialsNumber.length()==18){
            if(Integer.valueOf(credentialsNumber.substring(16,17))%2==0){
                sex=2;
            }
        }else if(credentialsNumber.length()==15){
            if(Integer.valueOf(credentialsNumber.substring(14,15))%2==0){
                sex=2;
            }
        }

        return sex;
    }


    public static void main(String[] args){
        System.out.println(isCredentialsNumber("35058319931031223"));
    }
}
